<?php

	class RequestBusiness {
		
		public function save($serviceRequest, $dataBaseConnection = null) {
			$connectionOpened = false;
			if ($dataBaseConnection == null) {
				$connectionOpened = true;
				$dataBaseConnection = DataAccessFactory::factory('DataAccessMySQL');
				$dataBaseConnection->connect();
				$dataBaseConnection->beginTransaction();
			}
			$requestDataAccess = DataAccessFactory::factory('RequestDataAccessMySQL');
			$result = $requestDataAccess->save($serviceRequest, $dataBaseConnection);
			
			if ($result != -1) {
				$requestItemBusiness = BusinessFactory::factory('RequestItemBusiness');
				foreach ($serviceRequest->getRequestItemList() as $requestItem) {
					$requestItem->setRequestId($result);
					$result2 = $requestItemBusiness->save($requestItem, $dataBaseConnection);
					if ($result2 == -1) {
						break;
					}
				}
			}
			
			if ($connectionOpened) {
				if ($result == -1 || $result2 == -1) {
					$dataBaseConnection->rollback();
				} else {
					$dataBaseConnection->commit();
				}
				$dataBaseConnection->disconnect();
			}
			
			return $result;
		}

		public function selectRequestsByUserId($userId) {
			
			$dataBaseConnection = DataAccessFactory::factory('DataAccessMySQL');
			
			$dataBaseConnection->connect();
			
			$requestDataAccess = DataAccessFactory::factory('RequestDataAccessMySQL');
			
			$result = $requestDataAccess->selectRequestsByUserId($userId, $dataBaseConnection);
			
			$dataBaseConnection->disconnect();
			
			return $result;		
		}


		public function updateRequest($serviceRequest, $dataBaseConnection = null) {
			$connectionOpened = false;
			if ($dataBaseConnection == null) {
				$connectionOpened = true;
				$dataBaseConnection = DataAccessFactory::factory('DataAccessMySQL');
				$dataBaseConnection->connect();
				$dataBaseConnection->beginTransaction();
			}
			
			$requestDataAccess = DataAccessFactory::factory('RequestDataAccessMySQL');
			
			$result = $requestDataAccess->updateRequest($serviceRequest, $dataBaseConnection);
			if ($connectionOpened) {
				if (!$result) {
					$dataBaseConnection->rollback();
				} else {
					$dataBaseConnection->commit();
				}
				$dataBaseConnection->disconnect();
			}
			
			return $result;
		}
		
		
		public function deleteRequest($serviceRequest, $dataBaseConnection = null) {
			$connectionOpened = false;
			if ($dataBaseConnection == null) {
				$connectionOpened = true;
				$dataBaseConnection = DataAccessFactory::factory('DataAccessMySQL');
				$dataBaseConnection->connect();
				$dataBaseConnection->beginTransaction();
			}
			
			$requestItemBusiness = BusinessFactory::factory('RequestItemBusiness');
			$result1 = $requestItemBusiness->deleteRequestItemByRequestId($serviceRequest->getRequestId(), $dataBaseConnection);
			
			$requestDataAccess = DataAccessFactory::factory('RequestDataAccessMySQL');
			$result2 = $requestDataAccess->deleteRequest($serviceRequest, $dataBaseConnection);
			
			if ($connectionOpened) {
				if (!$result1 || !$result2) {
					$dataBaseConnection->rollback();
				} else {
					$dataBaseConnection->commit();
				}
				$dataBaseConnection->disconnect();
			}
			
			return $result2;
		}
		
		public function selectRequestsByEmployeeId($employeeId) {
			
			$dataBaseConnection = DataAccessFactory::factory('DataAccessMySQL');
			
			$dataBaseConnection->connect();
			
			$requestDataAccess = DataAccessFactory::factory('RequestDataAccessMySQL');
			
			$result = $requestDataAccess->selectRequestsByEmployeeId($employeeId, $dataBaseConnection);
			
			$dataBaseConnection->disconnect();
			
			return $result;		
		}
		
	}

?>